package com.example.team6.controller;


import com.example.team6.mapper.AdminMapper;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwj
 * @since 2020-06-27
 */


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.team6.entity.*;
//import com.example.group6.mapper.MessageMapper;
import com.example.team6.service.AdminService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: yzh
 * @create: 2020-06-23 16:20
 **/
//@RequestMapping("/team6/admin")
@RestController
@CrossOrigin
public class AdminController {
    //    @Autowired
//    TeacherService teacherService;
//    @Autowired
//    StudentService studentService;
    @Autowired
    AdminService adminService;
    @Autowired
    public  AdminMapper adminMapper;

    // @Autowired
    // MessageMapper messageMapper;

    @PostMapping(value = "api/adminlogin")
    @ResponseBody
    public resultt adminlogin(@RequestBody Admin requestUser) {
        resultt resultt = new resultt();
        QueryWrapper<Admin> adminWrapper = new QueryWrapper<>();
        adminWrapper.eq("adm_account",requestUser.getAdmAccount());
       // System.out.println(requestUser.getAdmAccount());
        adminWrapper.eq("adm_pass", requestUser.getAdmPass());

        Admin admin = adminService.getOne(adminWrapper);
      //  System.out.println(admin);
        if (null == admin) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("adminid", admin.getAdminid());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/adminregister")
    @ResponseBody
    public resultt adminregister(@RequestBody Admin requestUser) {

        resultt resultt = new resultt();
        QueryWrapper<Admin> adminWrapper = new QueryWrapper<>();
        Admin admin = new Admin();
        admin.setAdmAccount(requestUser.getAdmAccount());
        admin.setAdmPass(requestUser.getAdmPass());
        Integer result = adminMapper.insert(admin);
//        adminWrapper.eq("adm_account",requestUser.getAdmAccount());
//        // System.out.println(requestUser.getAdmAccount());
//        adminWrapper.eq("adm_pass", requestUser.getAdmPass());
//
//        Admin admin = adminService.getOne(adminWrapper);
        //  System.out.println(admin);
//        adminService.insert(admin);
        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("adminid", admin.getAdminid());
            resultt.setData(map);
        }
        return resultt;
    }
}
